package com.syezon.wifikey.wifi;

import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.syezon.wifikey.R;
import com.syezon.wifikey.base.BaseActivity;
import com.syezon.wifikey.utils.StatusBarUtil;
import com.syezon.wifikey.wifi.dialog.AddBlackConfirmDialog;
import com.syezon.wifikey.wifi.info.WifiInfo;

import butterknife.BindView;
import butterknife.OnClick;

public class WifiDetailActivity extends BaseActivity {

    public static final String ARG_WIFI_INFO = "wifi_info";

    @BindView(R.id.rl_top)
    RelativeLayout mRlTop;
    @BindView(R.id.tv_ssid)
    TextView mTvSsid;
    @BindView(R.id.iv_wifi_icon)
    ImageView mIvWifi;
    @BindView(R.id.tv_wifi_connect_status)
    TextView mTvWifiConnectStatus;
    @BindView(R.id.tv_wifi_level)
    TextView mTvWifiLevel;
    @BindView(R.id.tv_wifiname)
    TextView mTvWifiName;
    @BindView(R.id.tv_signal)
    TextView mTvSignal;
    @BindView(R.id.tv_safe_type)
    TextView mTvSafeType;
    @BindView(R.id.tv_wifi_mac)
    TextView mTvWifiMac;

    private WifiInfo mWifiInfo;
    private AddBlackConfirmDialog addBlackConfirmDialog;

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        Parcelable wifiInfo = extras.getParcelable(ARG_WIFI_INFO);
        if (wifiInfo != null) {
            mWifiInfo = (WifiInfo) wifiInfo;
        } else {
            finish();
            return;
        }
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_wifi_detail;
    }

    @Override
    protected void initViewsAndEvents() {
        StatusBarUtil.setPaddingSmart(this, mRlTop);
        mTvSsid.setText(mWifiInfo.getSsid());
        if (mWifiInfo.getWifiCipherType() == WifiSupport.WifiCipherType.WIFICIPHER_NOPASS) {
            int level = mWifiInfo.getLevel();
            if (level == 4) {
                mIvWifi.setImageResource(R.drawable.icon_wifi01);
            }
            if (level == 3) {
                mIvWifi.setImageResource(R.drawable.icon_wifi02);
            }
            if (level == 2) {
                mIvWifi.setImageResource(R.drawable.icon_wifi03);
            }
            if (level == 1) {
                mIvWifi.setImageResource(R.drawable.icon_wifi04);
            }
            if (level == 0) {
                mIvWifi.setImageResource(R.drawable.icon_wifi05);
            }
        } else {
            int level = mWifiInfo.getLevel();
            if (level == 4) {
                mIvWifi.setImageResource(R.drawable.icon_wifi06);
            }
            if (level == 3) {
                mIvWifi.setImageResource(R.drawable.icon_wifi07);
            }
            if (level == 2) {
                mIvWifi.setImageResource(R.drawable.icon_wifi08);
            }
            if (level == 1) {
                mIvWifi.setImageResource(R.drawable.icon_wifi09);
            }
            if (level == 0) {
                mIvWifi.setImageResource(R.drawable.icon_wifi10);
            }
        }
        if (mWifiInfo.isConnected()) {
            mTvWifiConnectStatus.setText("已连接");
        } else {
            mTvWifiConnectStatus.setText("未连接");
        }
        mTvWifiName.setText(mWifiInfo.getSsid());
        mTvWifiLevel.setText(mWifiInfo.getSsid());
        mTvSafeType.setText(mWifiInfo.getWifiInfo());
        mTvWifiMac.setText(mWifiInfo.getMac());
        mTvSignal.setText(mWifiInfo.getSignalStrength() + "%");
    }

    @OnClick({R.id.iv_back, R.id.tv_black_name})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_black_name:
                showAddBlackWifiAlertDialog();
                break;
        }
    }

    private void showAddBlackWifiAlertDialog() {
        if (addBlackConfirmDialog == null) {
            addBlackConfirmDialog = new AddBlackConfirmDialog(this, R.style.Dialog);
            addBlackConfirmDialog.setWifiInfo(mWifiInfo);
            addBlackConfirmDialog.setOwnerActivity(this);
        }
        addBlackConfirmDialog.show();
    }
}
